package com.peanut.service.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;

import com.peanut.persistence.RoleMapper;
import com.peanut.persistence.UserMapper;

public class SecurityLoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
	protected final transient Logger log = Logger.getLogger(getClass());
	
	@Autowired
	public UserMapper userMapper;
	
	@Autowired
	public RoleMapper roleMapper;
	
	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws ServletException, IOException {
//		User springUser = (User)authentication.getPrincipal();
//		com.peanut.model.User user = userMapper.queryByAccount(springUser.getUsername());
//		List<Role> roleList = roleMapper.queryRolesByAccount(springUser.getUsername());
//		user.setRoles(roleList);
//		request.getSession().setAttribute(Constants.SESSION_LOGIN_USER, user);
		log.debug("登录且验证已成功--->"+authentication.getPrincipal());
		super.onAuthenticationSuccess(request, response, authentication);
	}
}
